﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.JShop.BLL;
using System.Data;
using System.Text;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Admin.Template.Operate
{
    public partial class TitleLinkPage : System.Web.UI.Page
    {
        public string _ColumnID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 102) || !BLL.BLLCheckAdmin.LoginOk(Page, 118) || !BLL.BLLCheckAdmin.LoginOk(Page, 119)) return;//判断页面权限
            if (!IsPostBack)
            {
                string _UnitID = Request.Params["UnitID"];
                string templateID = Request.Params["TemplateID"];
                string styleID = Request.Params["StyleID"];
                #region "属性"
                TxtCol.Attributes.Add("onkeydown", "return funKeyDown(event);");
                DropColumn.Attributes.Add("onchange", "ShowColumn(this);");
                TxtContent.Attributes.Add("onselect", "storeCaret(this);");
                TxtContent.Attributes.Add("onclick", "storeCaret(this);");
                TxtContent.Attributes.Add("onkeyup", "storeCaret(this);");
                #endregion
                CNVP.JShop.BLL.CNVP_UnitsInfo bll = new CNVP.JShop.BLL.CNVP_UnitsInfo();
                #region "样式选择"
                DataTable Dt_Style = bll.UnitStyleXmlList(Convert.ToInt16(styleID));
                //DataTable Dt_Style = bll.UnitStyleXmlList(Convert.ToInt16(templateID));
                int i = 0;
                StringBuilder Str = new StringBuilder();
                Str.Append("<script language=\"javascript\">\r\n");
                Str.Append("var strScriptCode = new Array();\r\n");
                foreach (DataRow Row in Dt_Style.Rows)
                {
                    ListItem _List = new ListItem(Row["Name"].ToString(), Row["ID"].ToString());
                    DropStyle.Items.Add(_List);
                    Str.Append("strScriptCode[" + i + "] = new Array();\r\n");
                    Str.Append("strScriptCode[" + i + "][0] = \"" + Row["ID"].ToString() + "\";\r\n");
                    Str.Append("strScriptCode[" + i + "][1] = \"" + Row["Name"].ToString() + "\";\r\n");
                    Str.Append("strScriptCode[" + i + "][2] = \"" + Row["Script"].ToString() + "\";\r\n");
                    Str.Append("strScriptCode[" + i + "][3] = \"" + Row["ShowHTML"].ToString() + "\";\r\n");
                    i++;
                }

                Str.Append("</script>\r\n");
                //注册脚本到样式选择下拉框
                DropStyle.Attributes.Add("onChange", "ChangeShow(this);");
                //注册JS脚本到页尾
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", Str.ToString());
                #endregion
                #region "读取属性"
                DataTable Dt = bll.GetUnitStyle(Convert.ToInt32(_UnitID), Convert.ToInt32(templateID));
                if (Dt != null && Dt.Rows.Count > 0)
                {
                    string Content = Dt.Rows[0]["UnitHtml"].ToString();
                    if (!string.IsNullOrEmpty(Content))
                    {
                        CNVP.JShop.Publish.LabelMass labelmass = new CNVP.JShop.Publish.LabelMass(Content);
                        labelmass.ParseContent();
                        TxtContent.Text = labelmass.Mass_Inserted;
                        if (labelmass.GetParamValue("NT:ShowDiv") == "True")
                        {
                            ChkShowDiv.Checked = true;
                        }
                        if (labelmass.GetParamValue("NT:IncludeChild") == "True")
                        {
                            cbIncludeChild.Checked = true;
                        }
                        DropStyle.SelectedValue = labelmass.GetParamValue("NT:StyleID");
                        DropColumn.SelectedValue = labelmass.GetParamValue("NT:ShowRoot");
                        TxtCol.Text = labelmass.GetParamValue("NT:ShowCol");
                        _ColumnID = labelmass.GetParamValue("NT:ColumnID");
                        if ((!string.IsNullOrEmpty(_ColumnID)) && (Public.IsNumber(_ColumnID)))
                        {
                            CNVP.JShop.BLL.CNVP_Column bll_column = new CNVP.JShop.BLL.CNVP_Column();
                            DataTable Dt_Column = bll_column.GetColumnInfo(Convert.ToInt32(_ColumnID));
                            if (Dt_Column != null && Dt_Column.Rows.Count > 0)
                            {
                                TxtColumnName.Text = Dt_Column.Rows[0]["ColumnName"].ToString();
                            }
                            Dt_Column.Dispose();
                            Dt_Column.Clear();

                            #region "注册脚本"
                            Type CsType = this.GetType();
                            ClientScriptManager _CsManager = Page.ClientScript;
                            string StrJs = "<script>$('#ShowColumn').css('display','');</script>";
                            _CsManager.RegisterStartupScript(CsType, "ButtonClickScript", StrJs, false);
                            #endregion
                        }
                    }
                }
                #endregion
                #region "HTML代码属性"
                DataTable Dt1 = bll.GetHTMLCodeProperty();
                StringBuilder HTMLCode = new StringBuilder();
                HTMLCode.Append("<select name=\"HTMLCode\" id=\"HTMLCode\" style=\"width:120px;\" onchange=\"insertAtCaret(this.form.TxtContent,this.value)\">\r\n");
                HTMLCode.Append("<option value=\"\">----HTML标签----</option>\r\n");
                foreach (DataRow Row in Dt1.Rows)
                {
                    HTMLCode.Append(string.Format("<option value=\"{1}\">{0}</option>\r\n", Row["ItemName"].ToString(), Row["ItemValue"].ToString()));
                }
                HTMLCode.Append("</select>\r\n");
                LitHTMLCode.Text = HTMLCode.ToString();
                #endregion
                #region "单元标签属性"
                StringBuilder LableCode = new StringBuilder();
                LableCode.Append("<select name=\"LabelCode\" id=\"LabelCode\" style=\"width:120px;\" onchange=\"insertAtCaret(this.form.TxtContent,this.value)\">\r\n");
                LableCode.Append("<option value=\"\">----单元标签----</option>\r\n");
                CNVP.JShop.BLL.LabelCode bll1 = new CNVP.JShop.BLL.LabelCode();
                DataTable Dt2 = bll1.GetProperty();
                foreach (DataRow Row in Dt2.Rows)
                {
                    if (Row["Type"].ToString() == "0" | Row["Type"].ToString() == templateID)
                    {
                        LableCode.Append(string.Format("<option value=\"{1}\">{0}</option>\r\n", Row["Name"].ToString(), Row["Script"].ToString()));
                    }
                }
                LableCode.Append("</select>\r\n");
                LitLabelCode.Text = LableCode.ToString();
                #endregion
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string _UnitID = Request.Params["UnitID"];
            string _StyleID = Request.Params["templateID"];
            string _DropStyleID = DropStyle.SelectedValue;
            string _ShowRoot = DropColumn.SelectedValue;
            string _HidColumnID = Request.Params["HidColumnID"];
            string _ShowCol = TxtCol.Text.Trim();
            string _Content = TxtContent.Text;
            string _ShowDiv = "False";
            string includeChild = "False";
            if (ChkShowDiv.Checked)
            {
                _ShowDiv = "True";
            }
            if (cbIncludeChild.Checked)
            {
                includeChild = "True";
            }
            if (!string.IsNullOrEmpty(_Content))
            {
                _Content = string.Format("[NT:UnLoop,NT:LabelType=NewsListPage,NT:StyleID={2},NT:ColumnID={5},NT:ShowDiv={1},NT:ShowRoot={3},NT:ShowCol={4},NT:IncludeChild={6}]{0}[/NT:UnLoop]", _Content, _ShowDiv, _DropStyleID, _ShowRoot, _ShowCol, _HidColumnID, includeChild);
            }
            CNVP.JShop.BLL.CNVP_UnitsInfo bll = new CNVP.JShop.BLL.CNVP_UnitsInfo();
            bll.UnitID = Convert.ToInt32(_UnitID);
            bll.StyleID = Convert.ToInt32(_StyleID);
            bll.UnitHtml = Public.FilterStr(_Content);
            bll.EditUnitStyle();
            MessageUtils.Write("<script language=\"javascript\" type=\"text/javascript\">top.parent.location.reload();</script>");
        }
    }
}
